package com.guigu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guigu.bean.Idea;
import com.guigu.bean.Msg;
import com.guigu.service.IdeaServices;

/**
 * @author 执子一白
 *控制灵感的CRUD请求
 */
@Controller
public class IdeaController {
	@Autowired
	IdeaServices ideaServices;
	
	/**
	 * 通过用户id查询所有灵感
	 * @param userid
	 * @return
	 */
	@RequestMapping("/allIdeas")
	@ResponseBody
	public Msg getAllIdeasByUserid(int userid) {
		List<Idea> ideas=ideaServices.getAllIdeas(userid);
		return Msg.success().add("ideas", ideas);
	}
	
	
	/**
	 * 灵感通过日期模糊查询
	 * @param vagueDate
	 * @param userId
	 * @return
	 */
	@RequestMapping("/findIdeas")
	@ResponseBody
	public Msg getIdeasByVagueDate(String vagueDate,@RequestParam(defaultValue="0")int userId){
		List<Idea> ideas=ideaServices.getIdeasByVagueDate(vagueDate, userId);
		if (ideas!=null&&!ideas.isEmpty()) {
			return Msg.success().add("ideas", ideas);
		}else {
			return Msg.fail().add("failInfo", "未查询到灵感");
		}
	}
	
	/**
	 * 灵感通过分享状态查询
	 * @param share
	 * @return
	 */
	@RequestMapping("/shareidea")
	@ResponseBody
	public Msg getIdeasByShare(String share){
		List<Idea> ideas=ideaServices.getIdeasByShare(share);
		if (ideas!=null&&!ideas.isEmpty()) {
			return Msg.success().add("ideas", ideas);
		}else {
			return Msg.fail().add("failInfo", "未查询到灵感");
		}
	}
	
	/**
	 * 添加灵感函数
	 * @param idea
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/ideaCRUD",method=RequestMethod.POST)
	@ResponseBody
	public Msg saveIdea(@Valid Idea idea,BindingResult result){
		if (idea==null||idea.getUserid()==null) {
			return Msg.fail().add("failInfo", "用户ID不可为空");
		}
		
		if (result.hasErrors()) {
			Map<String, Object> map =new HashMap<String, Object>();
			List<FieldError> errors =result.getFieldErrors();
			for (FieldError fieldError : errors) {
				System.out.println("错误字段名"+fieldError.getField());
				System.out.println("错误信息"+fieldError.getDefaultMessage());
				map.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			System.out.println("进入函数");
			return Msg.fail().add("errorFields", map).add("addStatus", "添加失败");
		}else {
			ideaServices.saveIdea(idea);
			return Msg.success().add("addStatus", "添加成功");
		}
	}
	
	/**
	 * 灵感修改功能
	 * @param idea
	 * @return
	 */
	@RequestMapping(value="/ideaCRUD/{ideaid}",method=RequestMethod.PUT)
	@ResponseBody
	public Msg updateIdea(Idea idea){
		ideaServices.updateIdea(idea);
		return Msg.success();
	}
	
	/**
	 * 灵感删除
	 * @param ideaid
	 * @return
	 */
	@RequestMapping(value="/ideaCRUD/{ideaid}",method=RequestMethod.DELETE)
	@ResponseBody
	public Msg deleteByIdeaId(@PathVariable("ideaid")Integer ideaId){
		ideaServices.deleteByIdeaId(ideaId);
		return Msg.success();
	}
}
